package com.itboye.boyeshoping

import android.annotation.SuppressLint
import android.graphics.Color
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentTransaction
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import com.itboye.boyeshoping.base.BaseActivity
import com.itboye.boyeshoping.fragment.CheapFragment
import com.itboye.boyeshoping.fragment.HomeFragment
import com.itboye.boyeshoping.fragment.MyFragment
import com.itboye.boyeshoping.fragment.SearchRabateFragment

class MainActivity : BaseActivity(), View.OnClickListener {


    private var fragmentManager: FragmentManager? = null
    private lateinit var homeLayout:RelativeLayout
    private lateinit var searchLayout:RelativeLayout
    private lateinit var cheapLayout:RelativeLayout
    private lateinit var myLayout:RelativeLayout

    private lateinit var homeIv:ImageView
    private lateinit var searchIv:ImageView
    private lateinit var cheapIv:ImageView
    private lateinit var myIv:ImageView

    private lateinit var homeTv:TextView
    private lateinit var searchTv:TextView
    private lateinit var cheapTv:TextView
    private lateinit var myTv:TextView

    private var homeFragment: HomeFragment? = null
    private var cheapFragment: CheapFragment? = null
    private var myFragment: MyFragment? = null
    private var searchRabateFragment: SearchRabateFragment? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main1)
        initFindView()
    }

    private fun initFindView() {
        homeLayout= findViewById(R.id.homeLayout) as RelativeLayout
        homeIv= findViewById(R.id.homeIv) as ImageView
        homeTv= findViewById(R.id.homeTv) as TextView
        searchLayout= findViewById(R.id.searchLayout) as RelativeLayout
        searchIv= findViewById(R.id.searchIv) as ImageView
        searchTv= findViewById(R.id.searchTv) as TextView
        cheapLayout= findViewById(R.id.cheapLayout) as RelativeLayout
        cheapIv= findViewById(R.id.cheapIv) as ImageView
        cheapTv= findViewById(R.id.cheapTv) as TextView
        myLayout= findViewById(R.id.myLayout) as RelativeLayout
        myIv= findViewById(R.id.myIv) as ImageView
        myTv= findViewById(R.id.myTv) as TextView

        homeLayout.setOnClickListener(this)
        searchLayout.setOnClickListener(this)
        cheapLayout.setOnClickListener(this)
        myLayout.setOnClickListener(this)
        fragmentManager = supportFragmentManager
        setTabSelection(0)
    }

    override fun onClick(p0: View?) {
       when(p0!!.id){
            R.id.homeLayout ->{//首页

              setTabSelection(0)
            }
           R.id.searchLayout ->{//搜返利

               setTabSelection(1)
           }
           R.id.cheapLayout ->{//白菜价

               setTabSelection(2)
           }
           R.id.myLayout ->{//我的
               setTabSelection(3)

           }

       }
    }
    /**
     * 根据传入的index参数来设置选中的tab页。
     * @param index  每个tab页对应的下标。0首页，1搜返利，2白菜价，3我的。
     */
    fun setTabSelection(index: Int) {
        // 每次选中之前先清楚掉上次的选中状态
        clearSelection()
        // 开启一个Fragment事务
       var transaction = fragmentManager!!.beginTransaction()
        // 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
        hideFragments(transaction)
        when (index) {
            0 -> {
//                dian = 0
                // 当点击了首页tab时，改变控件的图片和文字颜色
                homeIv!!.setImageResource(R.mipmap.home_selected)
                homeTv!!.setTextColor(resources.getColor(R.color.home_gray))
                if (homeFragment == null) {
                    // 如果MessageFragment为空，则创建一个并添加到界面上
                    homeFragment = HomeFragment()
                    transaction.add(R.id.fragments, homeFragment)
                } else {
                    // 如果MessageFragment不为空，则直接将它显示出来
                    transaction.show(homeFragment)
                }
            }
            1 -> {
//                dian = 0
                // 当点击了消息tab时，改变控件的图片和文字颜色
                searchIv!!.setImageResource(R.mipmap.sousuo_selected)
                searchTv!!.setTextColor(resources.getColor(R.color.maincolor))
                if (searchRabateFragment == null) {
                    // 如果ContactsFragment为空，则创建一个并添加到界面上
                    searchRabateFragment = SearchRabateFragment()
                    transaction.add(R.id.fragments, searchRabateFragment)
                } else {
                    // 如果ContactsFragment不为空，则直接将它显示出来
                    transaction.show(searchRabateFragment)
                }
            }
            2 -> {

                // 当点击了我的tab时，改变控件的图片和文字颜色
                cheapIv!!.setImageResource(R.mipmap.baicai_selected)
                cheapTv!!.setTextColor(resources.getColor(R.color.maincolor))
                if (cheapFragment == null) {
                    // 如果SettingFragment为空，则创建一个并添加到界面上
                    cheapFragment = CheapFragment()
                    transaction.add(R.id.fragments, cheapFragment)
                } else {
                    // 如果SettingFragment不为空，则直接将它显示出来
                    transaction.show(cheapFragment)
                }
            }
            3 -> {
//                dian = 0
                // 当点击了服务tab时，改变控件的图片和文字颜色
                myIv!!.setImageResource(R.mipmap.wode_selected)
                myTv!!.setTextColor(resources.getColor(R.color.maincolor))
                if (myFragment == null) {
                    // 如果NewsFragment为空，则创建一个并添加到界面上
                    myFragment = MyFragment()
                    transaction.add(R.id.fragments, myFragment)
                } else {
                    // 如果NewsFragment不为空，则直接将它显示出来
                    transaction.show(myFragment)
                }
            }

            else -> {
            }
        }
        transaction.commit()
    }
    private fun clearSelection() {
        homeIv!!.setImageResource(R.mipmap.shouye)
        homeTv!!.setTextColor(resources.getColor(R.color.home_gray))
        searchIv!!.setImageResource(R.mipmap.sousuo)
        searchTv!!.setTextColor(resources.getColor(R.color.home_gray))
        cheapIv!!.setImageResource(R.mipmap.baicai)
        cheapTv!!.setTextColor(resources.getColor(R.color.home_gray))
        myIv!!.setImageResource(R.mipmap.wode)
        myTv!!.setTextColor(resources.getColor(R.color.home_gray))

    }

    /**
     * 将所有的Fragment都置为隐藏状态。
     * @param transaction 用于对Fragment执行操作的事务
     */
    private fun hideFragments(transaction: FragmentTransaction) {
        if (homeFragment != null) {
            transaction.hide(homeFragment)
        }
        if (searchRabateFragment != null) {
            transaction.hide(searchRabateFragment)
        }
        if (cheapFragment != null) {
            transaction.hide(cheapFragment)
        }
        if (myFragment != null) {
            transaction.hide(myFragment)
        }

    }
}
